﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.Globalization;

using CNCSoft.EWinform;
using CNCSoft.EWinform.ControlUtils;
using CNCSoft.EWinform.AGrid;
using CNCSoft.EData;

using DevExpress.XtraGrid;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

using System.Threading;
using System.Globalization;

using Common;


namespace CNCSoft.ErpApps.TMS.Academic
{
    public partial class NhapTrangThai : Form
    {

        private BindingSource bindingSource1 = new BindingSource();
        private SqlDataAdapter dataAdapter = new SqlDataAdapter();
        DataSet dsHK = new DataSet();


        public NhapTrangThai()
        {
            // Sets the UI culture to VietNamese (VietNam) / English
            if (GlobalVariable.WSInfo.Language == "Tiếng Việt")
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("vi-VN");
            else if (GlobalVariable.WSInfo.Language == "English")
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");

            InitializeComponent();
        }


        

        private string strMaLop;
        
        //Lay gia tri ve tu user control thong qua event ValueChange
        private void userControl_ChonLOP1_ValueChanged(object sender,
                           CNCSoft.ErpApps.TMS.UserControl_ChonLOP.ValueChangedEventArgs e)
        {
            GetData(e.NewValue) ;
        }

        private void GetData(string strMaLop)
        {
            //if (selectCommand == "") return;

            try
            {
                SqlConnection cnn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);

                SqlCommand sqlCom = new SqlCommand("sp_Giaovu_CapNhatTrangThai", cnn);
                sqlCom.CommandType = CommandType.StoredProcedure;

                sqlCom.Parameters.Add(new SqlParameter("@pMaLop", SqlDbType.VarChar)).Value = strMaLop;
   
                SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCom);

                if (dsHK.Tables["TRANGTHAI"] != null)
                    dsHK.Tables["TRANGTHAI"].Clear();

                dataAdapter.Fill(dsHK, "TRANGTHAI");
              
                //table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                bindingSource1.DataSource = dsHK.Tables["TRANGTHAI"]; 

                SetEditMode(false);
                gridHK.Focus();


                if (dsHK.Tables["DM_TRANGTHAI"] != null)
                    dsHK.Tables["DM_TRANGTHAI"].Clear();
                dataAdapter = new SqlDataAdapter("SELECT * FROM DM_TRANGTHAI", GlobalVariable.WSInfo.ConnectionString);
                dataAdapter.Fill(dsHK, "DM_TRANGTHAI");
                
                repositoryItemLookUpEdit1.DataSource = dsHK.Tables["DM_TRANGTHAI"];

            }
            catch (SqlException ex)
            {
                MessageBox.Show("Error: \n" + ex.Message);
            }
        }

        
        private void SetEditMode(bool bEdit)
        {
            /*
            cboNienHoc.Enabled = !bEdit;
            cboLop.Enabled = !bEdit;
            cboHocKy.Enabled = !bEdit;
            cboMon.Enabled = !bEdit;

            cmdBoQua.Enabled = bEdit;
            cmdCapNhat.Enabled = bEdit;
            */
        }

        private void cmdUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection cnn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);

                string sqlUpdate = "UPDATE HOCSINH_LOP SET MaTrangThai = @pMaTrangThai " +
                                   "WHERE MaHocTap=@pMaHocTap";

                SqlParameter[] pUpdate = new SqlParameter[2];
                pUpdate[0] = new SqlParameter("@pMaTrangThai", SqlDbType.NVarChar, 2, "MaTrangThai");
                pUpdate[1] = new SqlParameter("@pMaHocTap", SqlDbType.VarChar, 50, "MaHocTap");

                SqlCommand cmdUpdate = new SqlCommand(sqlUpdate, cnn);
                cmdUpdate.Parameters.AddRange(pUpdate);

                dataAdapter.UpdateCommand = cmdUpdate;
                try
                {
                    dataAdapter.Update(dsHK, "TRANGTHAI");
                    dsHK.AcceptChanges();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

       
        private void cmdCancel_Click(object sender, EventArgs e)
        {
            GetData(userControl_ChonLOP1.MaLop);
        }

        private void NhapTrangThai_Load(object sender, EventArgs e)
        {
            gridHK.DataSource = bindingSource1;

            splitContainer1.SplitterDistance = 35;

            //Load user control chon LOP
            this.userControl_ChonLOP1 = new CNCSoft.ErpApps.TMS.UserControl_ChonLOP(GlobalVariable.WSInfo.getAppProperty("MaNienHocDefault"), GlobalVariable.WSInfo.ConnectionString);
            userControl_ChonLOP1.Top = 5;
            userControl_ChonLOP1.Left = 10;
            this.splitContainer1.Panel1.Controls.Add(userControl_ChonLOP1);

            userControl_ChonLOP1.ValueChanged += userControl_ChonLOP1_ValueChanged;

            GetData(userControl_ChonLOP1.MaLop);

            try
            {
                //this.Top = GlobalVariable.ActiveForm.Top + 40;
                //this.Height = GlobalVariable.ActiveForm.Height - 100;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

                

    }
}
